// pages/blood-pressure/blood-pressure.js
Page({
  data: {
    systolicValue: '',
    diastolicValue: '',
    heartRateValue: '',
    notesValue: '',
    showSystolicError: false,
    showDiastolicError: false,
    showHeartRateError: false,
    systolicError: '',
    diastolicError: '',
    heartRateError: ''
  },

  inputNotes(e) {
    this.setData({
      notesValue: e.detail.value
    });
  },

  checkSystolic(e) {
    const value = e.detail.value;
    this.setData({
      systolicValue: value
    });

    if (!value) {
      this.setData({
        showSystolicError: true,
        systolicError: '请输入收缩压'
      });
      return false;
    }

    const numValue = parseInt(value);
    if (isNaN(numValue) || numValue < 40 || numValue > 250) {
      this.setData({
        showSystolicError: true,
        systolicError: '收缩压范围应在40-250之间'
      });
      return false;
    }

    this.setData({
      showSystolicError: false
    });
    return true;
  },

  checkDiastolic(e) {
    const value = e.detail.value;
    this.setData({
      diastolicValue: value
    });

    if (!value) {
      this.setData({
        showDiastolicError: true,
        diastolicError: '请输入舒张压'
      });
      return false;
    }

    const numValue = parseInt(value);
    if (isNaN(numValue) || numValue < 30 || numValue > 150) {
      this.setData({
        showDiastolicError: true,
        diastolicError: '舒张压范围应在30-150之间'
      });
      return false;
    }

    this.setData({
      showDiastolicError: false
    });
    return true;
  },

  checkHeartRate(e) {
    const value = e.detail.value;
    this.setData({
      heartRateValue: value
    });

    if (!value) {
      this.setData({
        showHeartRateError: true,
        heartRateError: '请输入心率'
      });
      return false;
    }

    const numValue = parseInt(value);
    if (isNaN(numValue) || numValue < 30 || numValue > 200) {
      this.setData({
        showHeartRateError: true,
        heartRateError: '心率范围应在30-200之间'
      });
      return false;
    }

    this.setData({
      showHeartRateError: false
    });
    return true;
  },

  submitRecord(e) {
    // 验证表单
    const isSystolicValid = this.validateSystolic();
    const isDiastolicValid = this.validateDiastolic();
    const isHeartRateValid = this.validateHeartRate();

    if (!isSystolicValid || !isDiastolicValid || !isHeartRateValid) {
      return;
    }

    // 获取表单数据
    const { systolic, diastolic, heartRate, notes } = e.detail.value;

    // 创建记录对象
    const record = {
      id: Date.now().toString(),
      systolic: parseInt(systolic),
      diastolic: parseInt(diastolic),
      heartRate: parseInt(heartRate),
      notes: notes || '',
      recordTime: this.formatDateTime(new Date())
    };

    // 获取现有记录
    let bloodPressureRecords = wx.getStorageSync('bloodPressureRecords') || [];

    // 添加新记录到数组开头
    bloodPressureRecords.unshift(record);

    // 保存到本地存储
    wx.setStorageSync('bloodPressureRecords', bloodPressureRecords);

    // 显示成功提示
    wx.showToast({
      title: '记录保存成功',
      icon: 'success',
      duration: 1500,
      success: () => {
        // 返回上一页
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          });
        }, 1500);
      }
    });
  },

  validateSystolic() {
    const value = this.data.systolicValue;
    
    if (!value) {
      this.setData({
        showSystolicError: true,
        systolicError: '请输入收缩压'
      });
      return false;
    }

    const numValue = parseInt(value);
    if (isNaN(numValue) || numValue < 40 || numValue > 250) {
      this.setData({
        showSystolicError: true,
        systolicError: '收缩压范围应在40-250之间'
      });
      return false;
    }

    return true;
  },

  validateDiastolic() {
    const value = this.data.diastolicValue;
    
    if (!value) {
      this.setData({
        showDiastolicError: true,
        diastolicError: '请输入舒张压'
      });
      return false;
    }

    const numValue = parseInt(value);
    if (isNaN(numValue) || numValue < 30 || numValue > 150) {
      this.setData({
        showDiastolicError: true,
        diastolicError: '舒张压范围应在30-150之间'
      });
      return false;
    }

    return true;
  },

  validateHeartRate() {
    const value = this.data.heartRateValue;
    
    if (!value) {
      this.setData({
        showHeartRateError: true,
        heartRateError: '请输入心率'
      });
      return false;
    }

    const numValue = parseInt(value);
    if (isNaN(numValue) || numValue < 30 || numValue > 200) {
      this.setData({
        showHeartRateError: true,
        heartRateError: '心率范围应在30-200之间'
      });
      return false;
    }

    return true;
  },

  formatDateTime(date) {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');

    return `${year}-${month}-${day} ${hour}:${minute}`;
  },

  navigateBack() {
    wx.navigateBack({
      delta: 1
    });
  }
})